import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

import 'package:get/get.dart';
import 'package:smart_translator/app/event/DataCallBack.dart';
import 'package:smart_translator/app/views/controller/IndexViewController.dart';

import '../../../db/DbDAOHelper.dart';
import '../../../db/database.dart';
import '../../../routes/app_pages.dart';
import '../../../style/app_colors.dart';
import '../../../style/widget_style.dart';
import '../../../utils/CommonUtil.dart';
import '../../../widgets/AppHeader.dart';
import '../controllers/searchingTranscript_controller.dart.dart';

class SearchingtranscriptView extends GetView<SearchingtranscriptController> {
  SearchingtranscriptView({super.key});

  TextEditingController searchContentTextEditor = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: [
            Container(
              child: SvgPicture.asset(
                "assets/images/header_part_bg.svg",
                width: Get.width,
              ),
            ),
            AppHeader.obtainHeaderWithoutTitleAndRightPart(),
            Obx(()=>Container(
              width: Get.width,
              margin: EdgeInsets.only(top: 70.h),
              padding: EdgeInsets.all(20.w),
              child: Column(
                children: [
                  Container(
                    padding: EdgeInsets.only(left: 10.w),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      color: Colors.white,
                    ),
                    width: Get.width,
                    child: TextField(
                      controller: searchContentTextEditor,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        contentPadding:EdgeInsets.only(left: 10.w),
                        prefixIcon: SizedBox(
                          child: Container(
                            child: Image.asset('assets/images/icon_searchOrReplace_searching.png'),
                          ),
                          width: 16.w,
                          height: 16.h,
                        ),
                        prefixIconConstraints: BoxConstraints(
                          minWidth: 16.w, // 最小宽度
                          minHeight: 16.h, // 最小高度
                        ),
                        hintText: '输入搜索内容',
                        hintStyle: TextStyle(
                            fontSize: 14.sp
                        ),

                      ),
                      onChanged: (v) {
                        controller.searchingContent(v);
                      },

                    ),
                  ),
                  Expanded(child: controller.sessionList.isEmpty ? Container(
                    child: Column(
                      children: [
                        SizedBox(height: 80.h,),
                        Image.asset('assets/images/empty_list_realtime.png',width: 169.w,height: 91.h),
                        SizedBox(height: 16.h,),
                        Text('暂无搜索结果',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                      ],
                    ),
                  ) : ListView.builder(itemBuilder: (context, index) {
                    Session sessionItem = controller.sessionList.value[index];
                    return InkWell(
                      onTap: () {
                        DbDAOHelper().updateSessionHasReadById(sessionItem.id, true);
                        Get.toNamed(Routes.RECORDSCRIPT_DETAIL,parameters: {
                          'sessionId': sessionItem.id.toString(),
                          'audioType': 'old'
                        });
                      },
                      child: Column(
                        key: ValueKey(sessionItem.id),
                        children: [
                          Container(

                            padding: EdgeInsets.only(left: 15.w,right: 15.w,top: 13.h,bottom: 15.h),
                            decoration: BoxDecoration(
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black.withValues(alpha: 0.1),
                                  spreadRadius: 2.w,
                                  blurRadius: 7.w,
                                  offset: Offset(0, 8.sp), // changes position of shadow
                                ),
                              ],
                              color:Colors.white,
                              borderRadius: BorderRadius.circular(8), // Optional: add border radius
                            ),
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text('${sessionItem.session_name}',style: AppStyleThemeData.RECORD_SCRIPT_TEXT_STYLE),
                                    sessionItem.has_read ? SizedBox(): Container(
                                      width: 5.w,
                                      height: 5.h,
                                      decoration: BoxDecoration(
                                          shape: BoxShape.circle,
                                          color:Colors.red// Optional: add border radius
                                      ),
                                      child: SizedBox(),
                                    )
                                  ],
                                ),
                                SizedBox(height: 10.h,),
                                Container(
                                  child: Row(
                                    children: [
                                      Image.asset('assets/images/icon_record_script_canlender.png',width: 14.w,height: 14.h,),
                                      SizedBox(width: 5.w,),
                                      Text('${CommonUtil.formatCustomWeekday(sessionItem.create_time)}',style: AppStyleThemeData.HINT_TEXT_STYLE_small,),
                                      SizedBox(width: 16.w,),
                                      Image.asset('assets/images/icon_record_script_clock.png',width: 14.w,height: 14.h,),
                                      Text(CommonUtil.formatMillisecondsToHHMMSS2(sessionItem.audio_length),style: AppStyleThemeData.HINT_TEXT_STYLE_small),
                                    ],
                                  ),
                                ),
                                SizedBox(height: 10.h,),
                                Row(
                                  children: [
                                    Text('NOTE',style: AppStyleThemeData.HINT_TEXT_STYLE,)
                                  ],
                                )
                              ],
                            ),
                          ),
                          SizedBox(height: 20.h)
                        ],
                      ),
                    );
                  },
                    itemCount: controller.sessionList.length,
                    padding: EdgeInsets.all(0),
                  ),flex: 1,)
                ],
              ),
            ))
          ],
        )
    );
  }

}
